package com.xujl.mvcbind.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.xujl.mvcbind.bean.SplicingFragmentEntity;
import com.xujl.mvcbind.ui.custom.control.CustomAttrFragmentControl;
import com.xujl.mvcbind.ui.list.control.GridListFragmentControl;
import com.xujl.mvcbind.ui.loading.control.SimpleFragmentControl;
import com.xujl.mvcbind.widget.list.AbsFragmentAdapter;

import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

public class SplicingFragmentAdapter extends AbsFragmentAdapter<SplicingFragmentEntity> {
    public SplicingFragmentAdapter (List<SplicingFragmentEntity> dataList, FragmentManager fragmentManager) {
        super(dataList, fragmentManager);
    }

    @Override
    public Fragment createFragment (int position) {
        final SplicingFragmentEntity entity = getDataList().get(position);
        switch (entity.getType()) {
            case SplicingFragmentEntity.TYPE_TEXT:

                return SimpleFragmentControl.newInstance();
            case SplicingFragmentEntity.TYPE_IMAGE:

                return CustomAttrFragmentControl.newInstance();
            case SplicingFragmentEntity.TYPE_GRID:

                return GridListFragmentControl.newInstance();
            default:
                return null;
        }
    }

    @Override
    public View createFragmentView (View root, int position) {
        final SplicingFragmentEntity entity = getDataList().get(position);
        final FrameLayout frameLayout = new FrameLayout(root.getContext());
        switch (entity.getType()) {
            case SplicingFragmentEntity.TYPE_TEXT:
                frameLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));
                break;
            case SplicingFragmentEntity.TYPE_IMAGE:
                frameLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
                break;
            case SplicingFragmentEntity.TYPE_GRID:
                frameLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500));
                break;
            default:
                break;
        }
        frameLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return frameLayout;
    }
}
